package com.qf.fmall.utils;

import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

public class ReceiveEmail {
    public static void main(String[] args) {
        String host = "imap.qq.com"; // QQ邮箱IMAP服务器地址
        String username = "212135374@qq.com"; // 你的QQ邮箱地址
        String password = "xxmmfouyxlcubied"; // 你的QQ邮箱授权码

        try {
            // 设置邮件服务器属性
            Properties properties = new Properties();
            properties.setProperty("mail.store.protocol", "imap");
            properties.setProperty("mail.imap.host", host);
            properties.setProperty("mail.imap.port", "993");
            properties.setProperty("mail.imap.ssl.enable", "true");

            // 创建会话
            Session session = Session.getDefaultInstance(properties);

            // 连接到邮箱
            Store store = session.getStore("imap");
            store.connect(username, password);

            // 打开收件箱
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);

            // 获取邮件
            Message[] messages = inbox.getMessages();
            for (Message message : messages) {
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Content: " + message.getContent());
            }

            // 关闭连接
            inbox.close(false);
            store.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
